# Definition for an interval.
class Interval(object):
    def __init__(self, s=0, e=0):
        self.start = s
        self.end = e
    def __str__(self):
        return "[{},{}]".format(self.start, self.end)
    def __repr__(self):
        return "[{},{}]".format(self.start, self.end)

class Solution(object):
    def merge(self, intervals):
        """
        :type intervals: List[Interval]
        :rtype: List[Interval]
        """
        intervals.sort(key=lambda x : x.start)
        result = []
        current = None
        for interval in intervals:
            if not current: 
                current = interval
                continue
            if interval.start<=current.end:
                current.end = max(current.end, interval.end)
            else:
                result.append(current)
                current = interval
        if current:
            result.append(current)
        return result

print(Solution().merge([Interval(1,3), Interval(2,6), Interval(8, 10), Interval(15, 18)]))
                